﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace WindowsPhoneApplication1
{
    
    public partial class trackPage : PhoneApplicationPage
    {

        IsolatedStorageSettings trkData = IsolatedStorageSettings.ApplicationSettings;
        WindowsPhoneApplication1.ServiceReference1.Service1Client client;

        public trackPage()
        {
            InitializeComponent();
        }

        private void toMap_Click(object sender, RoutedEventArgs e)
        {
            
            NavigationService.Navigate(new Uri("/mapPage.xaml", UriKind.RelativeOrAbsolute));

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            if (trkData.Contains("SotredParams"))
            {
                if (trkData.Contains("name"))
                    name.Text = (String)trkData["name"];

                if (trkData.Contains("location"))
                    location.Text = (String)trkData["location"];

                if (trkData.Contains("difficulty"))
                    difficulty.Text = (String)trkData["difficulty"];

                if (trkData.Contains("accessibility"))
                    accessibility.Text = (String)trkData["accessibility"];

                try
                {
                    image1.Source = new BitmapImage(new Uri((String)trkData["picPath"], UriKind.RelativeOrAbsolute));
                }
                catch { };

                if (trkData.Contains("description"))
                    text.Text = (String)trkData["description"];

            }
            else
            {
                string nameStr = "";
                if (trkData.Contains("name"))
                    nameStr = (String)trkData["name"];

                client = new WindowsPhoneApplication1.ServiceReference1.Service1Client();
                client.retrieveTrackCompleted += new EventHandler<ServiceReference1.retrieveTrackCompletedEventArgs>(client_retrieveTrackCompleted);
                client.retrieveTrackAsync(nameStr);
            }
            if (!trkData.Contains("sourcePg"))
                trkData.Add("sourcePg", "recTrk");
            else
                trkData["sourcePg"] = "recTrk";
        }

        void client_retrieveTrackCompleted(object sender, ServiceReference1.retrieveTrackCompletedEventArgs e)
        {
            string ans = e.Result;
            string[] parameters = ans.Split(',');
           // result.Name + delim + result.City + delim + type.Difficulty + delim + type.Accessible + delim
            //                       + result.Longitude + delim + result.Latitued + delim + pic.Content + delim + result.Description;
            name.Text = parameters[0];
            location.Text = parameters[1];
            difficulty.Text = parameters[2];
            accessibility.Text = parameters[3];
            text.Text = parameters[parameters.Length - 1];
            try
            {
                image1.Source = new BitmapImage(new Uri((String)trkData["picPath"], UriKind.RelativeOrAbsolute));
            }
            catch {};
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            trkData.Remove("name");
            trkData.Remove("longitude");
            trkData.Remove("latitude");
            trkData.Remove("StoredParams");
        }

        private void comments_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/commentPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/addComPicPage.xaml", UriKind.RelativeOrAbsolute));
        }

    }

}